<?php
class Titles_model extends Model
{
	private $id;
	private $name;

	private $tableName;
	/*
	*	constructor
	*	@PARA: null
	*	@RETURN: null
	*/
	public function Titles_model()
	{
		parent::Model();
		$this->obj =& get_instance();
		$this->initTitles();
	}
	
	public function retrieveOptions()
	{
		$optData = array();
		$this->load->helper('userdefined');
		
		$titlesData = $this->find();
		$optData = convertToOptions($titlesData);
		return $optData;
	}	
	
	/*
	*	findAll
	*	@PARA: null
	*	@RETURN: null
	*/
	public function findAll()
	{
		return $this->find();
	}

	/*
	*	find
	*	@PARA: $filters
	*	@PARA: $start	
	*	@PARA: $count		
	*	@RETURN: $results
	*/
	function find($filters = NULL, $start = NULL, $count = NULL) 
	{
		$results = array();
		// Load the database library
		$this->load->database();
		// Filter could be an array or filter values or an SQL string.
		$where_clause = '';
		if ($filters) 
		{
			if ( is_string($filters) ) 
			{
				$where_clause = $filters;
			}
			elseif ( is_array($filters) ) 
			{
				// Build your filter rules
				if ( count($filters) > 0 ) 
				{
					foreach ($filters as $field => $value) 
					{
						$filter_list[] = " $field = '$value' ";
					}
					$where_clause = ' WHERE ' . join(' AND ', $filter_list );
				}
			}
		}

		$limit_clause = '';
		if ($start) 
		{
			if ($count) 
			{
				$limit_clause = " LIMIT $start, $count ";
			}
			else 
			{
				$limit_clause = " LIMIT $start ";
			}
		}

		// Build up the SQL query string and run the query
		$sql = 'SELECT * FROM titles ' . $where_clause . $limit_clause;
		$query = $this->db->query($sql);

		if ($query->num_rows() > 0) 
		{
			foreach ($query->result_array() as $row)      // Go through the result set
			{
				// Build up a list for each column from the database and place it in
				// ...the result set
				$query_results['id']		 = $row['id'];
				$query_results['name']		 = $row['name'];
				$results[]		 = $query_results;
			}
		}
		return $results;
	}
	  
	/*
	*	init_Titles
	*	@PARA: null
	*	@RETURN: null
	*/
	private function initTitles()
	{
		$this->id = '';
		$this->name = '';		
		$this->tableName = 'titles';
	}
}
?>